﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Controls_SelectPreUpProduct : System.Web.UI.UserControl
{
    public string p
    {
        set { Hidden1.Value = value; }
    }

    private bool IsUseColorSize
    {
        get
        {
            return new BLL.SYSPARAM().GetProductUpDownColorSizeConfig();
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    private void Bind()
    {
        if (IsUseColorSize)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ProductUpDown.spdm,isnull(CA_NAME,'') as spmc,guige1.ggdm as colorCode,guige1.ggmc as colorName,guige2.ggdm as sizeCode,guige2.ggmc as sizeName ");//spmc+'  '+
            strSql.Append("from ProductUpDown left join COMMODITY_AUTO ");
            strSql.Append("on ProductUpDown.spdm=COMMODITY_AUTO.ca_code left join shangpin ");
            strSql.Append("on ProductUpDown.spdm=shangpin.spdm left join guige1 ");
            strSql.Append("on ProductUpDown.GG1DM=guige1.ggdm left join guige2 ");
            strSql.Append("on ProductUpDown.GG2DM=guige2.ggdm ");
            strSql.Append("where ProductUpDown.IsPreUp=1 and ProductUpDown.ISUP=0 ");
            if (DropDownList1.SelectedValue == "0")
            {
                strSql.Append(" and ProductUpDown.spdm like '%");
                strSql.Append(TextBox1.Text.Trim());
                strSql.Append("%'");
            }
            else if (DropDownList1.SelectedValue == "1")
            {
                strSql.Append(" and CA_NAME like '%");
                strSql.Append(TextBox1.Text.Trim());
                strSql.Append("%'");
            }
            else if (DropDownList1.SelectedValue == "2")
            {
                strSql.Append(" and SPMC like '%");
                strSql.Append(TextBox1.Text.Trim());
                strSql.Append("%'");
            }

            ListBox1.Items.Clear();
            DataSet ds = new BLL.SHANGPIN().GetSPListSearch(strSql.ToString());
            ListItem item;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                item = new ListItem();
                item.Value = ds.Tables[0].Rows[i]["spdm"].ToString() + "~" + ds.Tables[0].Rows[i]["colorCode"].ToString() + "~" + ds.Tables[0].Rows[i]["sizeCode"].ToString();
                item.Text = ds.Tables[0].Rows[i]["spdm"].ToString() + " " + ds.Tables[0].Rows[i]["spmc"].ToString() + " " + ds.Tables[0].Rows[i]["colorName"].ToString() + " " + ds.Tables[0].Rows[i]["sizeName"].ToString();
                ListBox1.Items.Add(item);
            }
        }
        else
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ProductUpDown.spdm,ProductUpDown.spdm+ ' '+isnull(CA_NAME,'') as spmc ");
            strSql.Append("from ProductUpDown left join COMMODITY_AUTO ");
            strSql.Append("on ProductUpDown.spdm=COMMODITY_AUTO.ca_code ");
            strSql.Append("where ProductUpDown.IsPreUp=1 and ProductUpDown.ISUP=0 ");
            if (DropDownList1.SelectedValue == "0")
            {
                strSql.Append(" and ProductUpDown.spdm like '%");
                strSql.Append(TextBox1.Text.Trim());
                strSql.Append("%' ");
            }
            else if (DropDownList1.SelectedValue == "1")
            {
                strSql.Append(" and CA_NAME like '%");
                strSql.Append(TextBox1.Text.Trim());
                strSql.Append("%' ");
            }
            else if (DropDownList1.SelectedValue == "2")
            {
                strSql.Append(" and SPMC like '%");
                strSql.Append(TextBox1.Text.Trim());
                strSql.Append("%' ");
            }
            strSql.Append("GROUP BY ProductUpDown.spdm,CA_NAME");

            //DataSet ds = new BLL.SHANGPIN().GetSPListSearch(strSql.ToString());
            //ListBox1.DataSource = ds.Tables[0].DefaultView;
            //ListBox1.DataTextField = "SPMC";
            //ListBox1.DataValueField = "SPDM";
            //ListBox1.DataBind();

            ListBox1.Items.Clear();
            DataSet ds = new BLL.SHANGPIN().GetSPListSearch(strSql.ToString());
            ListItem item;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                item = new ListItem();
                item.Value = ds.Tables[0].Rows[i]["spdm"].ToString() + "~1~1";
                item.Text = ds.Tables[0].Rows[i]["spdm"].ToString() + " " + ds.Tables[0].Rows[i]["spmc"].ToString();
                ListBox1.Items.Add(item);
            }
        }
        
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        Bind();
    }
    

}
